// 定义一个store
import { defineStore } from "pinia";
import { requireImg } from "./img.js";

import { myMixin } from "./../mixin.js";
const { errorMsgBox, successMsgBox, apiAll, withLoading } = myMixin();

export const userStore = defineStore("main", {
    // 定义一个state
    state: () => {
        return {
            cartList: [],
            collectList: [],
            categorys: [
                {
                    id: 1,
                    name: "水果蔬菜",
                    img: new URL(`../assets/shuiguo.png`, import.meta.url).href, // require("../assets/shuiguo.png")
                    color: "#edf7f1",
                },
                {
                    id: 2,
                    name: "油",
                    img: new URL(`../assets/you.png`, import.meta.url).href,
                    color: "#fef5ed",
                },
                {
                    id: 3,
                    name: "面包",
                    img: new URL(`../assets/mianbao.png`, import.meta.url).href,
                    color: "#f4ebf7",
                },

                {
                    id: 4,
                    name: "鱼肉蛋",
                    img: new URL(`../assets/yurou.png`, import.meta.url).href,
                    color: "#fde8e4",
                },
                {
                    id: 5,
                    name: "奶制品",
                    img: new URL(`../assets/naizhipin.png`, import.meta.url).href,
                    color: "#fdf8e5",
                },
                {
                    id: 6,
                    name: "饮料",
                    img: new URL(`../assets/yinliao.png`, import.meta.url).href,
                    color: "#edf7fc",
                },
            ],
            products: [
                {
                    id: 1,
                    img: requireImg("pingguo.png"),
                    name: "苹果",
                    price: "10",
                    unit: "￥",
                    details: "可口香甜",
                    location: "北美",
                    star: 2.5,
                    category_id: 1,
                    isCollect: false,num:0
                },
                {
                    id: 2,
                    img: new URL(`../assets/xiangjiao.png`, import.meta.url).href,
                    name: "辣椒",
                    price: "20",
                    unit: "￥",
                    category_id: 1,
                    isCollect: false,num:0
                },
                {
                    id: 3,
                    img: new URL(`../assets/xiangjiao.png`, import.meta.url).href,
                    name: "香蕉",
                    price: "20",
                    unit: "￥",
                    category_id: 1,
                    isCollect: false,num:0
                },
                {
                    id: 4,
                    img: new URL(`../assets/xiangjiao.png`, import.meta.url).href,
                    name: "芒果",
                    price: "10",
                    unit: "￥",
                    category_id: 1,
                    isCollect: false,num:0
                },
                {
                    id: 5,
                    img: new URL(`../assets/jidan.png`, import.meta.url).href,
                    name: "鸡蛋",
                    price: "10",
                    unit: "￥",
                    category_id: 4,
                    isCollect: false,num:0
                },
                {
                    id: 6,
                    img: new URL(`../assets/chengzhi.png`, import.meta.url).href,
                    name: "橙汁",
                    price: "10",
                    unit: "￥",
                    category_id: 6,
                    isCollect: false,num:0
                },

                {
                    id: 7,
                    img: new URL(`../assets/baishikele.png`, import.meta.url).href,
                    name: "百事可乐",
                    price: "10",
                    unit: "￥",
                    category_id: 6,
                    isCollect: false,num:0
                },
                {
                    id: 8,
                    img: new URL(`../assets/xuebi.png`, import.meta.url).href,
                    name: "雪碧",
                    price: "10",
                    unit: "￥",
                    category_id: 6,
                    isCollect: false,num:0
                },
                {
                    id: 9,
                    img: new URL(`../assets/miantiao.png`, import.meta.url).href,
                    name: "面条",
                    price: "10",
                    unit: "￥",
                    category_id: 1,
                    isCollect: false,num:0
                },
                {
                    id: 10,
                    img: new URL(`../assets/kekoukele.png`, import.meta.url).href,
                    name: "可口可乐",
                    price: "10",
                    unit: "￥",
                    category_id: 6,
                    isCollect: false,num:0
                },
            ],
            num: 0,
            hot_products: [
                {
                    id: 1,
                    img: new URL(`../assets/pingguo.png`, import.meta.url).href,
                    name: "苹果",
                    price: "10",
                    unit: "￥",
                    isCollect: false,num:0
                },
                {
                    id: 2,
                    img: new URL(`../assets/xiangjiao.png`, import.meta.url).href,
                    name: "辣椒",
                    price: "40",
                    unit: "￥",
                    isCollect: false,num:0
                },
                {
                    id: 3,
                    img: new URL(`../assets/xiangjiao.png`, import.meta.url).href,
                    name: "香蕉",
                    price: "30",
                    unit: "￥",
                    isCollect: false,num:0
                },
                {
                    id: 4,
                    img: new URL(`../assets/xiangjiao.png`, import.meta.url).href,
                    name: "芒果",
                    price: "40",
                    unit: "￥",
                    isCollect: false,num:0
                },
                {
                    id: 5,
                    img: new URL(`../assets/jidan.png`, import.meta.url).href,
                    name: "鸡蛋",
                    price: "40",
                    unit: "￥",
                    isCollect: false,num:0
                },
                {
                    id: 6,
                    img: new URL(`../assets/chengzhi.png`, import.meta.url).href,
                    name: "橙汁",
                    price: "40",
                    unit: "￥",
                    isCollect: false,num:0
                },
            ],
        };
    },
    // 类似于计算属性
    getters: {

        cartNum(state) {
            //类似于计算属性
            state.cartList.forEach((element) => {
                state.num += element.num;
            });

            return state.num;
        },

        love(state) {
            return (love) => {
                return `${state.username}爱好是${love}`;
            };
        },
        //类似于计算属性
        getProductByCategory_id: (state) => {
            return (category_id) =>
                state.products.find((product) => product.category_id === category_id);
        },
    },
    actions: {
        //计算购物车总商品数量
        calcluteCart() {
            this.num = 0;
            console.log(JSON.stringify(this.cartList));
            this.cartList.forEach((element) => {
                if (!element.num) {
                    element.num = 0;
                }
                this.num += element.num;
            });
        },
    },
});
